package com.neu.task;


import com.neu.dao.HireDao;
import com.neu.entity.Hire;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class OrderTask {
    @Autowired
    private HireDao hireDao;
    //每秒执行
    //@Scheduled(cron = "0/1 * * * * ?")
    //每分钟执行
    //每天执行一次
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void processTimeoutOrder(){
        log.info("定时处理超时订单：{}", LocalDateTime.now());

        LocalDateTime time = LocalDateTime.now().plusHours(-24);
        //select * from hire where status = ? and time < ? (当前时间 - 24小时）
        List<Hire> hireList = hireDao.getByPayAndOrderTimeLT("Unpaid", time);

        if(hireList != null && hireList.size()>0){
            for(Hire hire:hireList){
                hireDao.updateOrderStatus(hire);

            }
        }

    }
}
